﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.IO;

namespace NewsReader
{
    public class RssProvider
    {
        

        public string Name { get; set; }
        public string Code { get; set; }
        public Dictionary<string, string> FeedUrls { get; set; }
        public override string ToString()
        {
            return Name ?? "";
        }

        public event EventHandler<GetRssFeedCompletedEventArgs> GetRssFeedCompleted;
        public void GetRssFeedAsync(string topic)
        {
            string url = FeedUrls[topic];
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.OpenReadAsync(new Uri(url, UriKind.RelativeOrAbsolute));
        }

        private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (GetRssFeedCompleted != null)
            {
                List<RssItem> rssItems = DataProvider.GetRssItems(e.Result);
                GetRssFeedCompletedEventArgs args = new GetRssFeedCompletedEventArgs(e.Error, rssItems, false, null);
                GetRssFeedCompleted.Invoke(sender, args);
            }
        }
    }
    public class RssFeedTopics
    {
        public const string TopStories = "Top stories";
        public const string World = "World";
        public const string Business = "Business";
        public const string Sports = "Sports";
        public const string Entertainment = "Entertainment";
        public const string Tech = "Tech";
        public const string Health = "Health";
        public const string Lifestyle = "Lifestyle";
    }
    public class GetRssFeedCompletedEventArgs : EventArgs
    {
        // Summary:
        //     Gets a value that indicates whether an asynchronous operation has been canceled.
        //
        // Returns:
        //     true if the asynchronous operation has been canceled; otherwise, false. The
        //     default is false.
        public bool Cancelled { get; private set; }
        //
        // Summary:
        //     Gets a value that indicates which error occurred during an asynchronous operation.
        //
        // Returns:
        //     An System.Exception instance, if an error occurred during an asynchronous
        //     operation; otherwise null.
        public Exception Error { get; private set; }
        //
        // Summary:
        //     Gets the unique identifier for the asynchronous task.
        //
        // Returns:
        //     An object reference that uniquely identifies the asynchronous task; otherwise,
        //     null if no value has been set.
        public object UserState { get; private set; }
        // Summary:
        //     Gets a readable stream that contains the results of the System.Net.WebClient.OpenReadAsync(System.Uri,System.Object)
        //     method.
        //
        // Returns:
        //     A System.IO.Stream that contains the results of the System.Net.WebClient.OpenReadAsync(System.Uri,System.Object)
        //     method.
        //
        // Exceptions:
        //   System.InvalidOperationException:
        //     The asynchronous request was cancelled.
        public IEnumerable<RssItem> Result { get; private set; }

        public GetRssFeedCompletedEventArgs(Exception error, IEnumerable<RssItem> result, bool cancelled, object userState)
        {
            Error = error;
            Result = result;
            Cancelled = cancelled;
            UserState = userState;
        }
    }
    
}

